//
//  YJBadgeView.m
//  CToolBox
//
//  Created by crj on 16/8/22.
//  Copyright © 2016年 oyf. All rights reserved.
//

#import "YJBadgeView.h"

#define kLeftSpace 5  //文字到红圈的距离
#define kTextFont  10
#define kBadgeMaxHeight 16 //有文字时的高度
#define kBadgeMinHeight 10 //没有文字时的高度

@interface YJBadgeView()

@property (nonatomic, strong) UILabel *badgeLabel;


@end

@implementation YJBadgeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.badgeLabel];
    }
    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    self.backgroundColor = [UIColor redColor];
    [_badgeLabel sizeToFit];
    _badgeLabel.center = CGPointMake(self.yj_width/2, self.yj_height/2);
}

- (void)refreshFrame{
    
    if(_badgeVaule == nil){
        self.hidden = YES;
    }else{
        self.hidden = NO;
        if([_badgeVaule length] == 0){
            self.frame = CGRectMake( 0, 0, kBadgeMinHeight, kBadgeMinHeight);
        }else{
            CGFloat labelWidth = [Utils widthOfString:_badgeVaule font:[UIFont systemFontOfSize:kTextFont] height:10];
            self.frame = CGRectMake(0, 0, labelWidth+kLeftSpace*2, kBadgeMaxHeight);
        }
    }
    
    _badgeLabel.center = CGPointMake(self.yj_width/2, self.yj_height/2);
}

- (void)setBadgeVaule:(NSString *)badgeVaule{
    
    _badgeVaule = badgeVaule;
    _badgeLabel.text = badgeVaule;
    [self refreshFrame];
}

- (UILabel *)badgeLabel{
    
    if(!_badgeLabel){
        _badgeLabel = [[UILabel alloc] init];
        _badgeLabel.textColor = [UIColor whiteColor];
        _badgeLabel.font = [UIFont systemFontOfSize:kTextFont];
    }
    return _badgeLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
